﻿using ORS.OrderRobot.Cache;
using ORS.OrderRobot.IOC;
using ORS.OrderRobot.ServiceContracts;
using ORS.OrderRobot.ServiceContracts.Dtos;
using ORS.OrderRobot.ServiceContracts.Dtos.Api;

namespace ORS.OrderRobot.Service
{
	/// <summary>
	/// 缓存统一设置读取类
	/// </summary>
	public class AccountCacheManager : ISingleton
    {
		private readonly ICache _cache;
        private readonly ISystemConfigService _systemConfigService;
        public AccountCacheManager(ICache cache, ISystemConfigService systemConfigService)
		{
			_cache = cache;
			_systemConfigService = systemConfigService;
		}
		/// <summary>
		/// 获取所有登机牌信息
		/// </summary>
		/// <returns></returns>
		public async Task<List<AccountBoardingDto>> GetAllBoarding()
		{
            var boardings = await _cache.GetAsync<List<AccountBoardingDto>>(Consts.CacheKey.AllBoarding) ?? new List<AccountBoardingDto>();
			return boardings;
        }
		/// <summary>
		/// 设置所有登机牌缓存
		/// </summary>
		/// <param name="latestBoardings"></param>
		/// <returns></returns>
		public async Task SetAllBoarding(List<AccountBoardingDto> latestBoardings)
		{
			await _cache.SetAsync(Consts.CacheKey.AllBoarding, latestBoardings, DateTime.Now.GetEndOfDayTimeSpan());
		}
		/// <summary>
		/// 获取账号和密码缓存
		/// </summary>
		/// <returns></returns>
		public async Task<Dictionary<string,string>?> GetAccounts()
		{
			var accounts = await _cache.GetAsync<Dictionary<string, string>>(Consts.CacheKey.Accounts);
			return accounts;
		}
        /// <summary>
        /// 设置账号和密码缓存
        /// </summary>
        /// <returns></returns>
        public async Task SetAccounts(Dictionary<string, string> accounts)
        {
            await _cache.SetAsync(Consts.CacheKey.Accounts,accounts,DateTime.Now.GetEndOfDayTimeSpan());
        }
		/// <summary>
		/// 获取某个登录用户
		/// </summary>
		/// <param name="email"></param>
		/// <returns></returns>
		public async Task<LoginResultInfo?> GetLogin(string email)
		{
			var key = Consts.CacheKey.GenAccountLoginKey(email);
			var account = await _cache.GetAsync<LoginResultInfo>(key);
			return account;
        }
        /// <summary>
        /// 设置某个登录用户
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public async Task SetLogin(string email, LoginResultInfo data)
        {
            var key = Consts.CacheKey.GenAccountLoginKey(email);
            var systemConfig = await _systemConfigService.GetConfigAsync();
            var interval = systemConfig.RefreshTokenTask.Interval;
            var intervalUnit = systemConfig.RefreshTokenTask.IntervalUnit;
            var timeSpan = intervalUnit switch
            {
                TimeUnit.MillionSecond => TimeSpan.FromMilliseconds(interval),
                TimeUnit.Second => TimeSpan.FromSeconds(interval),
                TimeUnit.Minute => TimeSpan.FromMinutes(interval),
                _ => TimeSpan.FromSeconds(interval)
            };
            await _cache.SetAsync(key,data,timeSpan);
        }
		/// <summary>
		/// 移除登录缓存
		/// </summary>
		/// <param name="email"></param>
		/// <returns></returns>
		public async Task RemoveLogin(string email)
		{
			var key = Consts.CacheKey.GenAccountLoginKey(email);
			await _cache.RemoveAsync(key);
        }
		/// <summary>
		/// 获取所有登录的缓存key
		/// </summary>
		/// <returns></returns>
		public async Task<List<string>> GetAllLoginKeys()
		{
            var list = await _cache.GetAllKeysAsync(Consts.CacheKey.AccountPrefix);
			return list.ToList();
        }

        public async Task<bool> HasOrder(string email)
        {
            var key = Consts.CacheKey.GenAccountTodayOrderKey(email, "");
            var order = await _cache.GetAsync<string>(key);
            return !string.IsNullOrEmpty(order);
        }

        public async Task SetOrder(string email,string orderId)
        {
            var key = Consts.CacheKey.GenAccountTodayOrderKey(email, "");
            await _cache.SetAsync(key,orderId, DateTime.Now.GetEndOfDayTimeSpan());
        }
    }
}

